//
//  单分类商品ListCtr.swift
//  smosky
//
//  Created by zhuxietong on 2018/7/12.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import soapp



public enum GoodsListMode {
    case cate(name:String,ID:String)
    case link(params:[String:Any])
}

extension Goods{
    
    open class GoodsListCtr:ASCollectionContainer{
        
        public var listMode:GoodsListMode = .link(params: [String:Any]())
        
        public let filter:Goods.Filter = Goods.Filter()
        
        open override var selector: TP.selector{
            let sec:TP.selector = TP.selector() + Goods.cellSelector
            return sec
        }
        
        public func updateMode(mode:GoodsListMode) {
            self.listMode = mode
            updateListWith()
        }
        
        open override func viewDidLoad() {
            self.page_load_section = "section0"
            self.content = ASTermsContainer.list(
                net: JoTask().post._surl(.商品列表).log.log_response(),
                node: "list",
                emptyMsg: "没有相关内容",
                params: nil,
                cellType: JoCellNode.self,
                detailType: Goods.DetailCtrType,
                test: false
            )
            
            collection_config = Eeconfig()
            super.viewDidLoad()
            
            collectionNode.backgroundColor = .white
            
            flowLayout.sectionHeadersPinToVisibleBounds = true
            //        flowLayout.headerReferenceSize = [Swidth,30]
            collectionNode.registerSupplementaryNode(ofKind: UICollectionView.elementKindSectionHeader)
            updateListWith()
            
            //        collectionNode.view.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
            
            //        collectionNode
            //        top_style.navBarBgStyle = .color(UIColor(shex: "#BFBFBF"))
            
            
        }
        
        
        
        public func updateListWith() {
            //
            //        //        print("--=======ss=\(filterValues)")
            //        let info = self.filterValues.mutable_dictionary
            //        switch info["name",""] {
            //        case "cate":
            //            filter.cate = filterValues["value"] as? [String:Any]
            //        case "searchKey":
            //            filter.searchKey = filterValues["value"] as? String
            //        case "order":
            //            filter.order = ["order":info["orderKey",""],"by":info["by",""]]
            //        default:
            //            break
            //        }
            switch self.listMode {
            case .link(params: let ps):
                paginator.parameters = ps
                title = ps.mutable_dictionary["__title__","推荐".loc]
            case .cate(name: let name, ID: let id):
                title = name
                filter.cate = ["cate":id]
                paginator.parameters = filter.params
            }
            reload_page()

            
        }
        
        open override func loadOnePage(list: [Any]) -> [Any] {
            for item in list {
                if let dict = item as? NSMutableDictionary{
                    Goods.ActiveType.markCellID(item: dict)
                }
            }
            return list
        }
        
        open override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            __post(notify: Goods.BaseCell.Notice.controllerWillAppear, object: self, userInfo: nil)
        }
        
        open override func viewWillDisappear(_ animated: Bool) {
            super.viewDidDisappear(animated)
            __post(notify: Goods.BaseCell.Notice.controllerDisAppear, object: self, userInfo: nil)
        }
        
        
        public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
            if section == 0
            {
                return Goods.listInset
            }
            return .zero
        }
        
    }
    
    
    
}

